﻿SET QUOTED_IDENTIFIER ON
GO
SET ARITHABORT ON
GO
SET NUMERIC_ROUNDABORT OFF
GO
SET CONCAT_NULL_YIELDS_NULL ON
GO
SET ANSI_NULLS ON
GO
SET ANSI_PADDING ON
GO
SET ANSI_WARNINGS ON
GO
CREATE PROCEDURE [dbo].[bhp_Utils_GetGroupInfo] 
	@GroupId	INT
AS
BEGIN

	SELECT
		COALESCE(g.Name, t.name) as GroupName,
		g.Identyficator,
		g.EnableTranslate,
		g.ReadOnly,
		ge.RecordExtension,
		ge.GroupExtension
	FROM
		n_Tree t
		LEFT JOIN bht_Groups g ON t.id = g.Id
		LEFT JOIN bht_GroupExtensions ge ON g.Id = ge.GroupId
	WHERE
		t.id = @GroupId

	SELECT
		t.id as ColumnId,
		CASE t.typ
			WHEN 'F' THEN 'F:'+ t.dat
			ELSE t.typ + '->' + (SELECT TOP 1 name FROM n_Tree WHERE id = t.rel) + ' (' + CAST(t.rel AS NVARCHAR(10)) + ')'
		END as DataType,
		COALESCE(c.DisplayName, t.name) as ColumnName,
		c.Description,
		c.HideForEdition,
		c.Sort,
		c.CustomEditControl,
		gt.TabName,
		gt.Sort
	FROM
		n_Tree t
		LEFT JOIN bht_Columns c ON t.id = c.ColumnId
		LEFT JOIN bht_GroupColumnsSplit gcs ON c.ColumnId = gcs.ColumnId
		LEFT JOIN bht_GroupTabs gt ON gcs.TabId = gt.TabId
	WHERE
		t.parent = @GroupId
		AND t.del = 0
	ORDER BY
		gt.Sort,
		gt.TabName,
		c.Sort,
		c.DisplayName
END